﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.IO;
using System.Collections;

namespace SystemContainer.Models
{
    public class DBInitializer : DropCreateDatabaseIfModelChanges<SystemEntities>
    {
        private FileReader reader;


        private Double toDouble(string input)
        {
            return Convert.ToDouble(input);
        }

        private int toInt(string input)
        {
            return Convert.ToInt32(input);
        }

        private List<List<string>> getVenueData()
        {
            reader = new FileReader(Constants.venueFilePath);
            List<List<string>> venues = reader.getList();
            reader = null;
            return venues;
        }

        private List<List<string>> getScenicLocationData()
        {
            reader = new FileReader(Constants.scenicLocationFilePath);
            List<List<string>> sceLocations = reader.getList();
            reader = null;
            return sceLocations;
        }

        private List<List<string>> getPhotoData()
        {
            reader = new FileReader(Constants.photoFilePath);
            List<List<string>> photos = reader.getList();
            reader = null;
            return photos;
        }

        private List<List<string>> getAOIData()
        {
            reader = new FileReader(Constants.AOIFilePath);
            List<List<string>> AOIs = reader.getList();
            reader = null;
            return AOIs;
        }

        protected override void Seed(SystemEntities context)
        {

            var list = new List<City>
            {
                new City{locationName = "New York",Latitude = 40.734771,  Longtitude = -74.003781},
                new City{locationName = "Los Angeles", Latitude = 34.0522,  Longtitude = -118.2456 }
            };
            list.ForEach(c => context.Cities.Add(c));
            context.SaveChanges();

            var fields = new List<Field>
            {
                new Field { Name = "Social-Multimedia" },
                new Field { Name = "Mobile-Video" }
            };

            fields.ForEach(a => context.Fields.Add(a));
            context.SaveChanges();

            var algorithms = new List<Algorithm>
            {
                new Algorithm { Name = "Mean-Shift", FieldID = 1 },
                new Algorithm { Name = "K-Means" ,FieldID = 1},
                new Algorithm { Name = "HITS" ,FieldID = 1}
            };

            algorithms.ForEach(a => context.Algorithms.Add(a));

            context.SaveChanges();

            /*
            ///read AOI file and construct AOI object and add AOI to database
            
            var AOIs = new List<AreaOfInterest>();
            List<List<string>> AOIData = getAOIData();
            int length = AOIData.Count;
            AreaOfInterest AOI;
            for (int i = 0; i < length; i++)
            {
                AOI = new AreaOfInterest { AlgorithmID = 1, RankAlgorithmID = 2,AreaOfInterestID = toInt(AOIData[i][0]), CityID = toInt(AOIData[i][4]), Longtitude = toDouble(AOIData[i][1]), Latitude = toDouble(AOIData[i][2]), rank = toInt(AOIData[i][3]) };
                AOIs.Add(AOI);
            }
            AOIs.ForEach(a => context.AreasOfInterests.Add(a));
            context.SaveChanges();


           
           /// read venue file and construct venue object and add venues to database
           
            var venues = new List<Venue>();
            List<List<string>> venueData = getVenueData();
            length = venueData.Count;
            Venue venue;
            for (int i = 0; i < length; i++)
            {
                venue = new Venue { type = LocationType.Venue, locationName = venueData[i][2], Longtitude = toDouble(venueData[i][3]), Latitude = toDouble(venueData[i][4]), AreaOfInterestID = toInt(venueData[i][5]) ,AOICityID = 1};
                venues.Add(venue);
            }
            venues.ForEach(v => context.Venues.Add(v));
            context.SaveChanges();

            
            /// read ScenicLocation file and construct ScenicLocation object and add ScenicLocation to database
            
            var sceLocations = new List<ScenicLocation>();
            List<List<string>> sceLocationData = getScenicLocationData();
            length = sceLocationData.Count;
            ScenicLocation location;
            for (int i = 0; i < length; i++)
            {
                location = new ScenicLocation {AlgorithmID= 1, ScenicLocationID = toInt(sceLocationData[i][2]), type = LocationType.ScenicLocation, Longtitude = toDouble(sceLocationData[i][0]), Latitude = toDouble(sceLocationData[i][1]), AreaOfInterestID = toInt(sceLocationData[i][3]), CityID = toInt(sceLocationData[i][4]), AOICityID = toInt(sceLocationData[i][4]) };
                sceLocations.Add(location);
            }
            sceLocations.ForEach(l => context.ScenicLocations.Add(l));
            context.SaveChanges();


             /// read photo file and construct photo object and add photos to database
            var photos = new List<Photo>();
            List<List<string>> photoData = getPhotoData();
            length = photoData.Count;
            Photo photo;
            for (int i = 0; i < length; i++)
            {
                photo = new Photo { Longtitude = toDouble(photoData[i][1]), Latitude = toDouble(photoData[i][2]), Name = photoData[i][3], scenicLocationID = toInt(photoData[i][4]) ,ScecCityID = 1};
                photos.Add(photo);
            }
            photos.ForEach(p => context.Photos.Add(p));
            
           
            
            

            /*var list = new List<Combination> 
            {
            new Combination {AlgorithmName = "算法1",AlgorithmDir = algorithmPath,CheckInDataName = "CheckInData1",CheckInDataPath = datasetPath,PhotoDataName = "PhotoData1",PhotoDataPath = datasetPath,City = "NewYork",ResultPath = resultPath },
            new Combination {AlgorithmName = "算法2",AlgorithmDir = algorithmPath,CheckInDataName = "CheckInData2",CheckInDataPath = datasetPath,PhotoDataName = "PhotoData2",PhotoDataPath = datasetPath,City = "Los Angeles",ResultPath = resultPath }
              
            };
            list.ForEach(l => context.list.Add(l));

            var cities = new List<City>
            { new City{ type=LocationType.City,locationName="New York", Latitude=40.69847032728747, Longtitude=-73.9514422416687},
                new City{ type=LocationType.City,locationName="Los Angeles",Latitude=34.0522, Longtitude=-118.2428}
            };


            cities.ForEach(l => context.Cities.Add(l));

            

            var areaOfInterests= new List<AreaOfInterest>
            {
                new AreaOfInterest
            {
                City = cities.Single(c=>c.locationName=="New York"),
                type = LocationType.AreaOfInterest,
                Latitude = 40.69847032728747,
                Longtitude = -73.9514422416687,
                locationName = "Location1"
            },
           new AreaOfInterest
            {
                 City = cities.Single(c=>c.locationName=="New York"),
                type = LocationType.AreaOfInterest,
                Latitude = 40.8705,
                Longtitude = -73.8000,
                locationName = "Location2"
            },
            new AreaOfInterest
            {
                City = cities.Single(c=>c.locationName=="New York"),
                type = LocationType.AreaOfInterest,
                Latitude = 40.80,
                Longtitude = -73.86,
                locationName = "Location3"
            },
            new AreaOfInterest
            {
                City = cities.Single(c=>c.locationName=="New York"),
                type = LocationType.AreaOfInterest,
                Latitude = 40.89,
                Longtitude = -73.90000,
                locationName = "Location4"
            }};
            areaOfInterests.ForEach(a => context.AreaOfInterests.Add(a));
            var CheckIns = new List<CheckInData>
            {
             new CheckInData { Name = "CheckInData1" , dir = "C:/"},
             new CheckInData { Name = "CheckInData12", dir = "C:/" },
             new CheckInData { Name = "CheckInData13", dir = "C:/" }

            };

            var Photos = new List<PhotoData>
            {
             new PhotoData { Name = "PhotoData1" , dir = "C:/"},
             new PhotoData { Name = "PhotoData3", dir = "C:/" },
             new PhotoData { Name = "PhotoData2", dir = "C:/" },

            };

            CheckIns.ForEach(c => context.CheckIns.Add(c));
            Photos.ForEach(a => context.Photos.Add(a));
            */

            /*    var venues = new List<Venue>
                {
                    new Venue
                    { AreaOfInterest= areaOfInterests.Single(c=>c.locationName=="location1"),
                        type = LocationType.Venue,
                    Latitude = 40.6984703272874766666666666666,
                    Longtitude = -73.9514422416687666666666666666,
                    locationName = "Venue1"
                    },
                    new Venue
                    { AreaOfInterest= areaOfInterests.Single(c=>c.locationName=="location1"),
                        type = LocationType.Venue,
                    Latitude = 40.6984703272874755555555555555555555555,
                    Longtitude = -73.951442241668755555555555555555555555,
                    locationName = "Venue2"
                    },
                    new Venue
                    { AreaOfInterest= areaOfInterests.Single(c=>c.locationName=="location1"),
                        type = LocationType.Venue,
                    Latitude = 40.69847032728747333333333333333333333,
                    Longtitude = -73.9514422416687333333333333333333333,
                    locationName = "Venue3"
                    },
                    new Venue
                    { AreaOfInterest= areaOfInterests.Single(c=>c.locationName=="location1"),
                        type = LocationType.Venue,
                    Latitude = 40.69847032728747999999999999999999,
                    Longtitude = -73.9514422416687999999999999999999,
                    locationName = "Venue4"
                    }
                };
                venues.ForEach(c => context.Venues.Add(c));

                var scenicLocations = new List<ScenicLocation>
                {
                     new ScenicLocation
                    { AreaOfInterest= areaOfInterests.Single(c=>c.locationName=="location1"),
                        type = LocationType.ScenicLocation,
                    Latitude = 40.698470327287472222222222222222222222,
                    Longtitude = -73.951442241668722222222222222222222222,
                    locationName = "SL1"
                    },
                      new ScenicLocation
                    { AreaOfInterest= areaOfInterests.Single(c=>c.locationName=="location1"),
                        type = LocationType.ScenicLocation,
                    Latitude = 40.6984703272874711111111111111111111111111111,
                    Longtitude = -73.9514422416687111111111111111111111111111111,
                    locationName = "SL2"
                    },
                    new ScenicLocation
                    { AreaOfInterest= areaOfInterests.Single(c=>c.locationName=="location1"),
                        type = LocationType.ScenicLocation,
                    Latitude = 40.6984703272874788888888888888888888888888888,
                    Longtitude = -73.95144224166878888888888888888888888888888888,
                    locationName = "SL3"
                    }
                };
                scenicLocations.ForEach(c => context.ScenicLocations.Add(c));
             */
            /*
            new List<Algorithm>
            {
                new Algorithm { Name = "The Best Of Men At Work", Field = fields.Single(g => g.Name == "Map"), dir = "C:/"},
                new Algorithm { Name = "A Copland Celebration, Vol. I", Field = fields.Single(g => g.Name == "Video") , dir = "C:/"},
                new Algorithm { Name = "Worlds", Field = fields.Single(g => g.Name == "Geo-tag"), dir = "C:/"},
                new Algorithm { Name = "For Those About To Mobile We Salute You", Field = fields.Single(g => g.Name == "Map"), dir = "C:/"},
                new Algorithm { Name = "Let There Be Mobile", Field = fields.Single(g => g.Name == "Map"), dir = "C:/"},
                new Algorithm { Name = "Balls to the Wall", Field = fields.Single(g => g.Name == "Map"), dir = "C:/"},
                new Algorithm { Name = "Restless and Wild", Field = fields.Single(g => g.Name == "Map"), dir = "C:/"},
                new Algorithm { Name = "Górecki: Symphony No. 3", Field = fields.Single(g => g.Name == "Video"), dir = "C:/"},
                new Algorithm { Name = "Big Ones", Field = fields.Single(g => g.Name == "Map"), dir = "C:/"},
                new Algorithm { Name = "Quiet Songs", Field = fields.Single(g => g.Name == "Geo-tag"), dir = "C:/"},
                new Algorithm { Name = "Jagged Little Pill", Field = fields.Single(g => g.Name == "Map"), dir = "C:/"},
                new Algorithm { Name = "Facelift", Field = fields.Single(g => g.Name == "Map"), dir = "C:/"},
                new Algorithm { Name = "Frank", Field = fields.Single(g => g.Name == "Geo-tag"), dir = "C:/"},
                new Algorithm { Name = "Ring My Bell", Field = fields.Single(g => g.Name == "Geo-tag"), dir = "C:/"},

                new Algorithm { Name = "Warner 25 Anos", Field = fields.Single(g => g.Name == "Geo-tag"), dir = "C:/"},
                new Algorithm { Name = "Plays Geo-taglica By Four Cellos", Field = fields.Single(g => g.Name == "Geo-tag"), dir = "C:/"},
                new Algorithm { Name = "Revelations", Field = fields.Single(g => g.Name == "Geo-tag"), dir = "C:/"},
                new Algorithm { Name = "Audioslave", Field = fields.Single(g => g.Name == "Map"), dir = "C:/"},
                new Algorithm { Name = "The Last Night of the Proms", Field = fields.Single(g => g.Name == "Video"), dir = "C:/"},
                new Algorithm { Name = "Sibelius: Finlandia", Field = fields.Single(g => g.Name == "Video"), dir = "C:/"},
                new Algorithm { Name = "Mozart: Symphonies Nos. 40 & 41", Field = fields.Single(g => g.Name == "Video"), dir = "C:/"},
                
                new Algorithm { Name = "Alcohol Fueled Brewtality Live! [Disc 1]", Field = fields.Single(g => g.Name == "Geo-tag"), dir = "C:/"},
                new Algorithm { Name = "Alcohol Fueled Brewtality Live! [Disc 2]", Field = fields.Single(g => g.Name == "Geo-tag"), dir = "C:/"},
                new Algorithm { Name = "Black Sabbath Vol. 4 (Remaster)", Field = fields.Single(g => g.Name == "Geo-tag"), dir = "C:/"},
                new Algorithm { Name = "Black Sabbath", Field = fields.Single(g => g.Name == "Geo-tag"), dir = "C:/"},
                new Algorithm { Name = "Carmina Burana", Field = fields.Single(g => g.Name == "Video"), dir = "C:/"},
                new Algorithm { Name = "A Soprano Inspired", Field = fields.Single(g => g.Name == "Video"), dir = "C:/"},
                new Algorithm { Name = "Chemical Wedding", Field = fields.Single(g => g.Name == "Geo-tag"), dir = "C:/"},
               }.ForEach(a => context.Algorithms.Add(a));



            
            Directory.CreateDirectory(algorithmDir);
            string[] alFiles = Directory.GetFiles(algorithmDir);

            var als =  new List<Algorithm>();
            foreach (var name in alFiles)
            {
                Algorithm a = new Algorithm { Name = name, Field = fields.Single(g => g.Name == "Mobile"), dir = datasetDir, datas = dataSets };
                als.Add(a);
            }
            als.ForEach(a => context.Algorithms.Add(a));

           /* {
                new Algorithm { Name = "The Best Of Men At Work", Field = fields.Single(g => g.Name == "Mobile"), dir = "C:/", datas = dataSets},
                new Algorithm { Name = "A Copland Celebration, Vol. I", Field = fields.Single(g => g.Name == "Video") , dir = "C:/", datas = dataSets},
                new Algorithm { Name = "Worlds", Field = fields.Single(g => g.Name == "Geo-tag"), dir = "C:/", datas = dataSets},
                new Algorithm { Name = "For Those About To Mobile We Salute You", Field = fields.Single(g => g.Name == "Mobile"), dir = "C:/", datas = dataSets },
                new Algorithm { Name = "Let There Be Mobile", Field = fields.Single(g => g.Name == "Mobile"), dir = "C:/", datas = dataSets},
                new Algorithm { Name = "Balls to the Wall", Field = fields.Single(g => g.Name == "Mobile"), dir = "C:/", datas = dataSets},
                new Algorithm { Name = "Restless and Wild", Field = fields.Single(g => g.Name == "Mobile"), dir = "C:/", datas = dataSets},
                new Algorithm { Name = "Górecki: Symphony No. 3", Field = fields.Single(g => g.Name == "Video"), dir = "C:/", datas = dataSets},
                new Algorithm { Name = "Big Ones", Field = fields.Single(g => g.Name == "Mobile"), dir = "C:/", datas = dataSets},
                new Algorithm { Name = "Quiet Songs", Field = fields.Single(g => g.Name == "Geo-tag"), dir = "C:/", datas = dataSets},
                new Algorithm { Name = "Jagged Little Pill", Field = fields.Single(g => g.Name == "Mobile"), dir = "C:/", datas = dataSets},
                new Algorithm { Name = "Facelift", Field = fields.Single(g => g.Name == "Mobile"), dir = "C:/", datas = dataSets},
                new Algorithm { Name = "Frank", Field = fields.Single(g => g.Name == "Geo-tag"), dir = "C:/", datas = dataSets},
                new Algorithm { Name = "Ring My Bell", Field = fields.Single(g => g.Name == "Geo-tag"), dir = "C:/", datas = dataSets},

                }
            */
            //  context.SaveChanges();
        }
    }

}